package com.paic.icore.test.demo.lock;

import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * describe:
 *
 * @author huxingjie
 * @date 2019/05/06
 * @since 3.6
 */
@Component
public class RedissonLockUtil {
    @Autowired
    private RedissonClient redissonSingle;

    public Boolean acquire(String lockKey, TimeUnit timeUnit, int waitTime, int leaseTime) {
        RLock lock = redissonSingle.getLock(lockKey);
        try {
            return lock.tryLock(waitTime, leaseTime, timeUnit);
        } catch (InterruptedException e) {
            return false;
        }
    }

    public void unlock(String lockkey) {
        RLock lock = redissonSingle.getLock(lockkey);
        lock.unlock();
    }
}

    